<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="./js/jquery-3.1.1.js"></script>
    <title>用户注册页面</title>
    <style>
        body {
            background-image: url("https://img.51miz.com/Element/00/59/91/19/f4c69dcc_E599119_0ff87615.jpg");
            background-repeat: no-repeat;
            background-size: cover;
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
        }

        .container {
            width: 300px;
            padding: 16px;
            background-color: white;
            margin: 100px auto;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        input[type=text], input[type=email], input[type=password] {
            width: 100%;
            padding: 12px 20px;
            margin: 8px 0;
            display: inline-block;
            border: 1px solid #ccc;
            box-sizing: border-box;
        }

        button {
            background-color: #333;
            color: white;
            padding: 14px 20px;
            margin: 8px 0;
            border: none;
            cursor: pointer;
            width: 100%;
        }

        button:hover {
            opacity: 0.8;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>用户注册</h1>
    <form id="register">
        <label><b>用户名</b></label>
        <input type="text" placeholder="输入用户名" name="username" required>

        <label><b>邮箱</b></label>
        <input type="email" placeholder="输入邮箱" name="email" required>

        <label><b>密码</b></label>
        <input type="password" placeholder="输入密码" name="userpassword" required>

        <label><b>重复密码</b></label>
        <input type="password" placeholder="再次输入密码" id="psw-repeat" required>
    </form>

    <button onclick="register() " type="button">注册</button>
</div>
</body>
<script>
    function register() {
        // val() 获取标签值
        var username = $('input[name="username"]').val();
        var email = $('input[name="email"]').val();
        var password = $('input[name="userpassword"]').val();
        var repeatPassword = $('input[id="psw-repeat"]').val();
        console.log(password);
        console.log(repeatPassword);

        if (password !== repeatPassword) {
            alert('两次输入的密码不一致！');
            return false;
        }

        $.post("http://localhost:8080/register",$("#register").serialize(),function (data){
            console.log(data);
            if (data === "注册成功"){
                alert('注册成功，用户名：' + username + '，邮箱：' + email);
                location.href = "login.html";
            }else {
                alert( data);
            }

        });

    };

</script>
</html>